
local msamacro="pho seattle chi sandiego denver sanfran dc vegas"
use csla,clear
gen city="la"
foreach j in `msamacro' {
append using cs`j'
replace city="`j'" if city==""
}


sort my0
merge my0 using housepricepredictors,nokeep
tab _merge
drop _merge
sort my0
merge my0 using housepricepredictors_allstates,nokeep
tab _merge
drop _merge
drop cs*


sort city my0

gen urate=urate_az if city=="pho"
replace urate=urate_wa if city=="seattle"
replace urate=urate_ca if city=="la"
replace urate=urate_ca if city=="sandiego"
replace urate=urate_ca if city=="sanfran"
replace urate=urate_il if city=="chi"
replace urate=urate_co if city=="denver"
replace urate=urate_dc if city=="dc"
replace urate=urate_nv if city=="vegas"

gen seas=mod(my0,12)

forvalues x=2/13{
sort city my0
local q=(`x'-2)*4+1
gen chngcs=delta0-delta0[_n-`x'] if city==city[_n-`x']
gen lagchngcs=delta0[_n-`x']-delta0[_n-2*`x'] if city[_n-`x']==city[_n-2*`x']
gen lag2chngcs=delta0[_n-2*`x']-delta0[_n-3*`x'] if city[_n-2*`x']==city[_n-3*`x']
gen lag3chngcs=delta0[_n-3*`x']-delta0[_n-4*`x'] if city[_n-3*`x']==city[_n-4*`x']
gen lag4chngcs=delta0[_n-4*`x']-delta0[_n-5*`x'] if city[_n-4*`x']==city[_n-5*`x']


gen chngurate=ln(urate[_n-`x'])-ln(urate[_n-2*`x']) if city[_n-`x']==city[_n-2*`x']
gen chngmrate=ln(mortgagerate[_n-`x'])-ln(mortgagerate[_n-2*`x']) if city[_n-`x']==city[_n-2*`x']


gen lurate=ln(urate[_n-`x'])
gen lmrate=ln(mortgagerate[_n-`x'])


gen resid=.
gen chngcs`q'hat=.
local msamacro="la pho seattle chi sandiego denver sanfran dc vegas"
foreach j in `msamacro' {
xi: qui reg chngcs lagchngcs lag2chngcs lag3chngcs lurate lmrate i.seas if city=="`j'" & my0<=254
predict residt if my0>=(255) & city=="`j'" & my0<=298,r
predict chngcs`x'hatt if my0>=(255)& my0<=298 & city=="`j'"
replace resid=residt if my0>=(255)& my0<=298& city=="`j'"
replace chngcs`q'hat=chngcs`x'hatt if my0>=(255) & my0<=298 & city=="`j'"
drop residt chngcs`x'hatt
}


gen rmseat=(resid)^2
egen rmsea=mean(rmseat)
replace rmsea=sqrt(rmsea)
gen maeat=abs(resid)
egen maea=mean(maeat)

display `x'
tab rmsea maea
drop rmse* resid* mae* chngcs lag* chngurate chngmrate lurate lmrate
}


rename my0 my
keep chngcs1hat* chngcs5hat* chngcs9hat* chngcs13hat* chngcs17hat* chngcs21hat* chngcs25hat* chngcs29hat* chngcs33hat* chngcs37hat* chngcs41hat* chngcs45hat*  my city

sort my city

save forecastingresults,replace
